#!/usr/bin/env python
# encoding: utf-8

"""
@version: v1.0
@author: majx2
@license: Apache Licence
@contact: majx2@midea.com
@site: http://www.crazyxing.com
@software: PyCharm
@file: Red_Wars.py
@time: 2/5/19 09:59
@description：抢红包
"""

# -*- encoding=utf8 -*-
__author__ = "majx2"

from airtest.core.api import *
from poco.drivers.android.uiautomation import AndroidUiautomationPoco

poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)

auto_setup(__file__)


def get_red_package():
    """
    发现红包，就立马执行点击操作
    :return:
    """
    if exists(Template(r"tpl1556030451168.png", record_pos=(-0.286, 0.583), resolution=(576, 1024))):
        touch(Template(r"tpl1556030451168.png", record_pos=(-0.146, -0.349), resolution=(576, 1024)))
    if exists(Template(r"tpl1556017512849.png", record_pos=(0.0, 0.28), resolution=(576, 1024))):
        print('新红包，抢抢抢~')
        touch(Template(r"tpl1556017512849.png", record_pos=(0.0, 0.28), resolution=(576, 1024)))
    #keyevent('BACK')
    touch(Template(r"tpl1556030205370.png", record_pos=(-0.438, -0.771), resolution=(576, 1024)))

    # 1.获取消息列表元素


#     msg_list_elements_pre = poco("android.widget.ListView").children()

# 1.1 由于msg_list_elements属于UIObjectProxy对象，不能使用reverse进行反转
# 利用For把消息取反
#     msg_list_elements = []

#     for item in msg_list_elements_pre:
#         msg_list_elements.insert(0, item)

# 2.从后面的消息开始遍历
#     for msg_element in msg_list_elements:

# 2.1 微信红包标识元素


#         red_key_element = msg_element.offspring('com.tencent.mm:id/apf')

# 2.2 是否已经领取元素
#         has_click_element = msg_element.offspring('com.tencent.mm:id/ape')

# 2.3 红包【包含：收到的红包和自己发出去的红包】
#         if red_key_element:
#             print('发现一个红包')
#             if has_click_element.exists() and (
#                     has_click_element.get_text() == '已领取' or has_click_element.get_text() == '已被领完'):
#                 print('已经领取过了，略过~')
#                 continue
#             else:
#                 # 抢这个红包
#                 print('新红包，抢抢抢~')
#                 msg_element.click()

#                 click_element = poco("com.tencent.mm:id/cv0")
#                 if click_element.exists():
#                     click_element.click()
#                 keyevent('BACK')
#         else:
#             print('红包元素不存在')
#             continue

# 1.打开微信
poco(text='微信').click()

# 2.输入要抢的关键字
# target = input('要抢哪个群:')
# target = '[微信红包]'

# 3.获取消息列表名称
while True:
    if (exists(Template(r"tpl1556018135066.png", record_pos=(-0.035, -0.286), resolution=(576, 1024)))):
        touch(Template(r"tpl1556018135066.png", record_pos=(-0.035, -0.286), resolution=(576, 1024)))

        # item_elements = poco(name='com.tencent.mm:id/b6g', textMatches='.*:\[微信红包\].*$')
        # if item_elements:
        #   for item in item_elements:
        #      item.click()
        try:
            get_red_package()
            touch(Template(r"tpl1556019051374.png", record_pos=(-0.444, -0.771), resolution=(576, 1024)))
        except Exception:
            keyevent('HOME')
            poco(text='微信').click()

    sleep(1)


